﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Public_EmiCalculator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        //if (!IsPostBack){
             
        //}
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            CalculateEMI();
            //return;
        }
    }

    protected void CalculateEMI()
    {

        double loanAmount = 0, annualInterestRate = 0, interestRate = 0, monthlyPayment = 0;
        int loanTermInYear = 0, totalInstalment = 0;

        loanAmount = Convert.ToDouble(txtLoanAmount.Text); // Total Loan Amount
        annualInterestRate = Convert.ToDouble(txtInterestRate.Text); // 4%, 10% per annum etc
        loanTermInYear = Convert.ToInt32(txtLoanTermInYear.Text); // 2 years, 3 years etc
        totalInstalment = loanTermInYear * 12; // Loan Term or Total Loan Term, or Total Installment
        interestRate = annualInterestRate / (100 * 12); // General Interest Rate

        monthlyPayment = Math.Round((loanAmount * interestRate / (1 - Math.Pow((1 + interestRate), (-totalInstalment)))), 2); // Calculate EMI or Monthly Payment Formula

        txtEMI.Text = monthlyPayment.ToString(); 

    }
 
}